/**
 * Author:yuiop
 * Date:2022/8/31 14:29
 */
package com.devops.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.devops.admin.service.DOSDatasourceService;
import com.devops.admin.service.DOSDynamicSQLService;
import com.devops.admin.service.DOSMetricsService;
import com.devops.admin.util.Assert;
import com.devops.admin.vo.DOSMetricsVo;
import com.devops.common.utils.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/metrics")
@Api(value = "/metrics",tags = "Metrics相关接口")
public class MetricsController {
    @Autowired
    DOSDynamicSQLService dosDynamicSQLService;
    @Autowired
    DOSMetricsService dosMetricsService;
    @Autowired
    DOSDatasourceService dosDatasourceService;

    /*@GetMapping("/executeSQL")
    @ApiOperation(value="动态sql",notes = "输入sql和dbName动态查询sql")
    @ApiImplicitParams({@ApiImplicitParam(name = "sql",value = "需要执行的sql",required = true,dataType = "String",paramType = "param"),
            @ApiImplicitParam(name = "dbName",value = "数据库名",required = true,dataType = "String",paramType = "param")})
    public Message executeSQL(@RequestParam String sql,@RequestParam String dbName) {
        Assert.isTrueMvc(StrUtil.isNotBlank(sql),"sql不能为空");
        Assert.isTrueMvc(StrUtil.isNotBlank(dbName),"dbName不能为空");
        List<Map<String,Object>> list = dosDynamicSQLService.executeSql(dbName,sql);
        return Message.ok(list);
    }*/

    @GetMapping("/show/{metricsName}")
    @ApiOperation(value="获取单个指标-String",notes = "根据metricsName获取单个指标")
    @ApiImplicitParams({@ApiImplicitParam(name = "metricsName",value = "metricsName",required = true,dataType = "String",paramType = "path")})
    public String getMetricsByName(@PathVariable("metricsName") String metricsName) throws IOException {
        Assert.isTrueMvc(StrUtil.isNotBlank(metricsName),"metricsName不能为空");
        return dosMetricsService.getMetricsByName(metricsName);
    }

    @PostMapping("/test")
    @ApiOperation(value="测试单个指标-String",notes = "根据metrics完整信息测试单个指标")
    @ApiImplicitParams({@ApiImplicitParam(name = "dosMetricsVo",value = "DOSMetricsVo类型数据",required = true,dataType = "DOSMetricsVo",paramType = "body")})
    public String testMetrics(@RequestBody DOSMetricsVo dosMetricsVo) throws IOException {
        return dosMetricsService.testMetrics(dosMetricsVo);
    }

    @GetMapping("/show-json/{metricsName}")
    @ApiOperation(value="获取单个指标-JSON",notes = "根据metricsName获取单个指标")
    @ApiImplicitParams({@ApiImplicitParam(name = "metricsName",value = "metricsName",required = true,dataType = "String",paramType = "path")})
    public Message getMetricsByNameJson(@PathVariable("metricsName") String metricsName) throws IOException {
        Assert.isTrueMvc(StrUtil.isNotBlank(metricsName),"metricsName不能为空");
        return Message.ok(dosMetricsService.getMetricsByName(metricsName));
    }

    @PostMapping("/test-json")
    @ApiOperation(value="测试单个指标-JSON",notes = "根据metrics完整信息测试单个指标")
    @ApiImplicitParams({@ApiImplicitParam(name = "dosMetricsVo",value = "DOSMetricsVo类型数据",required = true,dataType = "DOSMetricsVo",paramType = "body")})
    public Message testMetricsJson(@RequestBody DOSMetricsVo dosMetricsVo) throws IOException {
        return Message.ok(dosMetricsService.testMetrics(dosMetricsVo));
    }

    @GetMapping("/checkDB/{dbName}")
    @ApiOperation(value="检查数据源并添加",notes = "根据dbName检查数据源，若不存在则添加")
    @ApiImplicitParams({@ApiImplicitParam(name = "dbName",value = "dbName",required = true,dataType = "String",paramType = "path")})
    public boolean checkDB(@PathVariable("dbName") String dbName){
        return dosDatasourceService.checkDB(dbName);
    }
}
